﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DSAClient.Models.Input;

namespace DSAClient
{
    public partial class AllMapForm : BaseForm
    {
        Rectangle m_Rect;
        bool canMove;
        private int mouseLocationX = 0;          //初始化鼠标当前X坐标
        private int mouseLocationY = 0;          //初始化鼠标当前Y坐标

        private const int AreaWidth = 930;       //pictureBox控件的最大宽度
        private const int AreaHeight = 625;     //pictureBox控件的最大高度
        public Point point = new Point();
        ImageInfo imgInfo;
        public AllMapForm()
        {
            InitializeComponent();
        }

        public AllMapForm(Image img, float w, float h, ImageInfo imgInfo)
        {
            InitializeComponent();
            pictureBox1.Image = img;
            this.imgInfo = imgInfo;
            //m_Rect = new Rectangle(0, 0, 0, 0);           //初始化矩形裁剪选框

            //m_Rect.Width = (int)(pictureBox1.Width * 0.8f);
            //m_Rect.Height = (int)(pictureBox1.Height * 0.8f);

            ////居中显示
            //m_Rect.X = (pictureBox1.Width - m_Rect.Width) / 2;
            //m_Rect.Y = (pictureBox1.Height - m_Rect.Height) / 2;
            PropertyInfo _ImageRectanglePropert = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);

            Rectangle rectTemp = (Rectangle)_ImageRectanglePropert.GetValue(pictureBox1, null);



            AdjustmentImgRotation(img, rectTemp, w, h);
        }

        /// <summary>
        /// 调整图片旋转，实现最少留白
        /// </summary>
        /// <param name="imageList"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        protected void AdjustmentImgRotation(Image image, Rectangle rect, float w, float h)
        {
            if (rect.Width / rect.Height > w / h)
            {
                image.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
            if (AreaWidth / w <= AreaHeight / h)
            {
                int width = AreaWidth;
                int height = (int)(h * AreaWidth / w);
                m_Rect = new Rectangle((AreaWidth - width) / 2, (AreaHeight - height) / 2, width, height);
            }
            else
            {
                int width = (int)(w * AreaHeight / h);
                int height = AreaHeight;
                m_Rect = new Rectangle((AreaWidth - width) / 2, (AreaHeight - height) / 2, width, height);
            }
            //imageInfo.RectPoint = imageInfo.Rect.Location;
        }


        private void sbtnXuanz1_Click(object sender, EventArgs e)
        {
            try
            {
                Image img = pictureBox1.Image;
                img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                pictureBox1.Image = img;

                if (imgInfo != null && imgInfo.ImgPath != "")
                {
                    imgInfo.RotateNum = (imgInfo.RotateNum + 1) % 4;
                }
            }
            catch
            { }
        }

        private void sbtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (m_Rect != null)
                g.DrawRectangle(new Pen(new SolidBrush(Color.Red), 1), this.m_Rect);
        }

        //pictureBox内鼠标单击事件响应，保证鼠标在矩形选框内才可响应鼠标移动事件
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            Point pt = pictureBox1.PointToClient(Control.MousePosition);
            if (pt.X < m_Rect.X || pt.X > m_Rect.X + m_Rect.Width || pt.Y < m_Rect.Y || pt.Y > m_Rect.Y + m_Rect.Height)
                canMove = false;
            else
            {
                canMove = true;
                mouseLocationX = pt.X;    //更新当前鼠标X坐标
                mouseLocationY = pt.Y;    //更新当前鼠标Y坐标
            }
        }

        //鼠标矩形选框内移动事件响应，保证矩形选框始终在pictureBox控件内部
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (canMove)
            {
                Point pt = pictureBox1.PointToClient(Control.MousePosition);    //获取鼠标相对pictureBox控件的坐标
                m_Rect.X += pt.X - mouseLocationX;
                m_Rect.Y += pt.Y - mouseLocationY;
                mouseLocationX = pt.X;
                mouseLocationY = pt.Y;

                m_Rect.X = m_Rect.X < 0 ? 0 : m_Rect.X;
                m_Rect.Y = m_Rect.Y < 0 ? 0 : m_Rect.Y;
                m_Rect.X = m_Rect.X + m_Rect.Width > pictureBox1.Width ? pictureBox1.Width - m_Rect.Width : m_Rect.X;
                m_Rect.Y = m_Rect.Y + m_Rect.Height > pictureBox1.Height ? pictureBox1.Height - m_Rect.Height : m_Rect.Y;

                pictureBox1.Invalidate();
            }
        }

        //鼠标按键弹起事件响应，保证鼠标没有按键按下时无法响应鼠标移动事件
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            canMove = false;
        }

        private void sbtnSave_Click(object sender, EventArgs e)
        {
            //计算小图的坐标点
            point.X = m_Rect.Location.X * 299 / 930;
            point.Y = m_Rect.Location.Y * 201 / 625;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}
